{include file="header.html"}
<body class="gr">

<div class="navBar">
	<a class="left" href="wap.php?app=member&act=index"><i class="return"></i></a>
	<div class="titleView">休闲馆订单</div>
</div>

<div class="member-order main-padding-top">
	<div class="pullRefresh overflow-scroll" status="{if isset($status)}{$status}{/if}">
		<header class="ge-bottom ge-light">
			<ul class="switchView">
				<li {if !isset($status)}class="this"{/if}><a href="wap.php?app=member&act=coffee_order">全部</a></li>
				<li {if isset($status) && $status==0}class="this"{/if}><a href="wap.php?app=member&act=coffee_order&status=0">待付款</a></li>
				<li {if isset($status) && $status==1}class="this"{/if}><a href="wap.php?app=member&act=coffee_order&status=1">待派送</a></li>
				<li {if isset($status) && $status==2}class="this"{/if}><a href="wap.php?app=member&act=coffee_order&status=2">已派送</a></li>
			</ul>
		</header>
		
		{if count($data)>0}
		<ul class="list tableView tableView-noLine">
			{foreach from=$data name=g item=g}
			<li>
				<div class="view">
					<a href="?app=member&act=coffee_order_detail&id={$g->id}">
						<div class="status ge-bottom ge-light status{if $g->status>1}2{else}{$g->status}{/if}"><span>订单号：{$g->order_sn}</span><div>{$g->status}</div></div>
						<ul>
							{foreach from=$g->coffees name=p item=p}
							<li>
								<div class="gview">
									<div class="right"><span class="price">￥{number_format($p->price,2)}</span><span>× {$p->quantity}</span></div>
									<div class="pic" style="background-image:url({$p->coffee_pic});"></div>
									<div class="name">{$p->coffee_name}</div>
									<div class="spec"></div>
								</div>
							</li>
							{/foreach}
						</ul>
					</a>
					<div class="total" mid="{$g->id}">
						{if $g->status==0}
						<a class="pay" href="javascript:void(0)" pay_method="{$g->pay_method}"><span>马上付款</span></a>
						<form class="pay_form" action="wap.php?app=member&act=coffee_order_pay" method="post">
						<input type="hidden" name="re_order_sn" value="" />
						<input type="hidden" name="order_sn" value="{$g->order_sn}" />
						<input type="hidden" name="price" value="{$g->total_price}" />
						<input type="hidden" name="body" value="商富商城-咖啡订单" />
						</form>
						{/if}
						{if $g->status>=2}
						<a class="again ok" href="javascript:void(0)"><span>再次下单</span></a>
						<form class="again_form" action="api.php?app=coffee_cart&act=add_cart" method="post">
						<textarea style="display:none;">{json_encode($g->coffees)}</textarea>
						<input type="hidden" name="gourl" value="wap.php?app=coffee_cart&act=index" />
						<input type="hidden" name="coffees" class="coffees" value="" />
						</form>
						{/if}
						总计：<strong>￥{number_format($g->total_price,2)}</strong>
					</div>
				</div>
			</li>
			{/foreach}
		</ul>
		{else}
		<div class="norecord">当前没有任何订单</div>
		{/if}
	</div>
</div>

{include file="footer.html"}
<script>
var offset = $('.pullRefresh .list > li').length;
function createHtml(g){
	var html = '<li>\
		<div class="view">\
			<a href="?app=member&act=coffee_order_detail&id='+g.id+'">\
				<div class="status ge-bottom ge-light status'+(g.status>1?2:g.status)+'"><span>订单号：'+g.order_sn+'</span><div>'+statusName(g.status)+'</div></div>\
				<ul>';
				for(var i=0; i<g.coffees.length; i++){
				html += '<li>\
						<div class="gview">\
							<div class="right"><span class="price">￥'+g.coffees[i].price.numberFormat(2)+'</span><span>× '+g.coffees[i].quantity+'</span></div>\
							<div class="pic" style="background-image:url('+g.coffees[i].coffee_pic+');"></div>\
							<div class="name">'+g.coffees[i].coffee_name+'</div>\
							<div class="spec"></div>\
						</div>\
					</li>';
				}
				html += '</ul>\
			</a>\
			<div class="total" mid="'+g.id+'">';
				if(g.status==0){
				html += '<a class="pay" href="javascript:void(0)" pay_method="'+g.pay_method+'"><span>马上支付</span></a>\
				<form class="pay_form" action="wap.php?app=member&act=coffee_order_pay" method="post">\
				<input type="hidden" name="re_order_sn" value="" />\
				<input type="hidden" name="order_sn" value="'+g.order_sn+'" />\
				<input type="hidden" name="price" value="'+g.total_price+'" />\
				<input type="hidden" name="body" value="商富商城-咖啡订单" />\
				</form>';
				}
				if(g.status>=2){
				html += '<a class="again ok" href="javascript:void(0)"><span>再次下单</span></a>\
				<form class="again_form" action="api.php?app=coffee_cart&act=add_cart" method="post">\
				<textarea style="display:none;">'+$.jsonString(g.coffees)+'</textarea>\
				<input type="hidden" name="gourl" value="wap.php?app=coffee_cart&act=index" />\
				<input type="hidden" name="coffees" class="coffees" value="" />\
				</form>';
				}
				html += '总计：<strong>￥'+g.total_price.numberFormat(2)+'</strong>';
			html += '</div>\
		</div>\
	</li>';
	offset++;
	return html;
}
function statusName(status){
	var name = '';
	switch(status*1){
		case -3:name = '已退货';break;
		case -2:name = '已退款';break;
		case -1:name = '取消';break;
		case 0:name = '待付款';break;
		case 1:name = '待派送';break;
		case 2:
		case 3:
		case 4:name = '已派送';break;
	}
	return name;
}
function setInfo(){
	$('.gview .pic').loadbackground();
	$('.tableView .total strong').priceFont('bigPrice');
	$('.gview .right span.price').priceFont('bigPrice');
}
$(function(){
	$('.member-order').height($.window().height-$('.member-order').padding().top);
	$('.switchView').switchView({ column:'column', index:$('.switchView .this').index() });
	$('.tableView .status div').html(function(i, html){ return statusName(html) });
	setInfo();
	$(document.body).on('click', 'a.pay', function(){
		if($.browser.wechat && $(this).attr('pay_method')!='wxpay' && $(this).attr('pay_method')!='wxpay_h5'){
			alert('在微信上只能使用微信支付\n该订单下单时使用了支付宝方式');
			return false;
		}
		$(this).parent().find('.pay_form').submit();
	});
	$(document.body).on('click', 'a.again', function(){
		$.overload(null);
		var count = 0, data = $(this).parent().find('.again_form textarea').val().formatJSON(), arr = [];
		if(!$.isArray(data))return false;
		for(var i=0; i<data.length; i++){
			var coffees = { coffee_id:data[i].coffee_id, quantity:1 };
			arr.push(coffees);
			count++;
			if(count == data.length){
				var val = { coffees:arr };
				$(this).parent().find('.coffees').val($.jsonString(val));
				$(this).parent().find('.again_form').submit();
			}
		}
	});
	$('.pullRefresh').pullRefresh({
		header : true,
		footer : true,
		footerNoMoreText : '- END -',
		refresh : function(fn){
			var _this = this;
			offset = 0;
			$.getJSON('api.php?app=member&act=coffee_order&status='+_this.attr('status'), function(json){
				if(json.error!=0){ $.overloadError(json.msg);return }
				var html = '';
				if($.isArray(json.data))for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
				_this.find('.tableView').html(html);
				setInfo();
				fn();
			});
		},
		load : function(fn){
			var _this = this;
			$.getJSON('api.php?app=member&act=coffee_order&status='+_this.attr('status'), { offset:offset }, function(json){
				if(json.error!=0){ $.overloadError(json.msg);return }
				var html = '';
				if($.isArray(json.data))for(var i=0; i<json.data.length; i++)html += createHtml(json.data[i]);
				_this.find('.tableView').append(html);
				setInfo();
				fn();
			});
		}
	});
});
</script>
